<template>
	<view class="privacy-policy-page">
		<view class="header">
			<text class="title">{{ privacyData.title || '隐私政策' }}</text>
		</view>

		<scroll-view class="content-container" scroll-y>
			<view class="content">
				<rich-text :nodes="privacyData.content || '正在加载隐私政策内容...'"></rich-text>
			</view>
		</scroll-view>

		<view class="footer">
			<button class="btn-disagree" @click="handleDisagree">不同意</button>
			<button class="btn-agree" @click="handleAgree">同意并继续</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				privacyData: {
					title: '隐私政策',
					content: '',
					version: '1.0.0'
				}
			}
		},
		onLoad() {
			// 获取存储的隐私政策内容
			const tempData = uni.getStorageSync('temp_privacy_content');
			if (tempData) {
				this.privacyData = tempData;
				// 清除临时存储
				uni.removeStorageSync('temp_privacy_content');
			} else {
				// 如果没有临时数据，重新获取
				this.fetchPrivacyContent();
			}
		},
		onBackPress(event) {
			// 防止用户通过返回键绕过隐私政策同意
			if (event.from === 'backbutton') {
				this.handleDisagree();
				return true;
			}
		},
		onNavigationBarButtonTap() {
			// 防止用户通过导航栏返回按钮绕过隐私政策同意
			this.handleDisagree();
		},
		methods: {
			// 获取隐私政策内容
			fetchPrivacyContent() {
				const app = getApp();
				app.$core.get({
					url: '/xilujob.user/help_detail/id/4',
					loading: true,
					success: (ret, data) => {
						if (ret.code === 1 && ret.data) {
							this.privacyData.title = ret.data.title;
							this.privacyData.content = ret.data.content;
						}
					},
					fail: () => {
						uni.showToast({
							title: '获取隐私政策失败',
							icon: 'none'
						});
					}
				});
			},

			// 处理用户同意
			handleAgree() {
				// 保存同意记录
				uni.setStorageSync('privacy_policy_agreed', true);
				uni.setStorageSync('privacy_policy_version', this.privacyData.version);
				uni.setStorageSync('privacy_policy_agreed_time', new Date().toISOString());

				uni.showToast({
					title: '感谢您的同意',
					icon: 'success'
				});

				// 返回上级页面
				setTimeout(() => {
					uni.navigateBack();
				}, 1000);
			},

			// 处理用户拒绝
			handleDisagree() {
				uni.showModal({
					title: '提示',
					content: '您需要同意隐私政策才能使用本应用',
					confirmText: '重新考虑',
					cancelText: '退出应用',
					success: (res) => {
						if (res.cancel) {
							// 退出应用
							// #ifdef APP-PLUS
							plus.runtime.quit();
							// #endif
							// #ifdef H5
							window.close();
							// #endif
						}
						// 如果选择重新考虑，什么都不做，继续停留在当前页面
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.privacy-policy-page {
		height: 100vh;
		display: flex;
		flex-direction: column;
		background-color: #ffffff;
	}

	.header {
		padding: 40rpx 30rpx 20rpx;
		background-color: #2FC971;

		.title {
			color: #ffffff;
			font-size: 36rpx;
			font-weight: bold;
			text-align: center;
			display: block;
		}
	}

	.content-container {
		flex: 1;
		padding: 30rpx;
	}

	.content {
		line-height: 1.6;
		font-size: 28rpx;
		color: #333333;

		/* 设置rich-text的样式 */
		/deep/ rich-text {
			word-wrap: break-word;
			word-break: break-all;
		}
	}

	.footer {
		padding: 30rpx;
		display: flex;
		gap: 20rpx;
		background-color: #f8f8f8;
		border-top: 1rpx solid #e5e5e5;

		button {
			flex: 1;
			height: 80rpx;
			border-radius: 40rpx;
			font-size: 30rpx;
			border: none;
		}

		.btn-disagree {
			background-color: #cccccc;
			color: #666666;
		}

		.btn-agree {
			background-color: #2FC971;
			color: #ffffff;
		}
	}
</style>